import { auth as http } from '@zh/common-utils/http'
import { store } from '../../store'

import UrlUtil from '@zh/common-utils/utils/url'

export { http }

const client = { 1: 'admin', 2: 'user', 3: 'mobile' }

/**
 * 登录
 * @param {*} params
 * @returns
 */
export function login (params, config) {
  const validateCode = params.validateCode
  delete params.validateCode
  config = config || {
    needToken: false,
    headers: { validateCode }
  }
  const clientType = store.getAppConfig('_clientType', '')
  return http.post(`/sys/sso/login?type=${clientType || ''}`, params, config)
}

/**
 * 登录sso
 * @param {*} params
 * @returns
 */
export function loginsso (params, config) {
  const validateCode = params.validateCode
  delete params.validateCode
  config = config || {
    needToken: false,
    headers: { validateCode },
    handleError: false
  }
  const clientType = store.getAppConfig('_clientType', '')
  return http.post(`/sys/sso/loginsso?type=${clientType || ''}`, params, config)
}

/**
 * 获取加密公钥
 * @param {*} params
 * @returns
 */
export function loginKey () {
  return http.get('/sys/sso/loginKey', { needToken: false })
}

/**
 * 退出登录
 * @returns
 */
export function logout () {
  return http.get('/sys/sso/logout')
}

/**
 * 换 Token
 * @param {*} token
 * @returns
 */
export function exchangeToken (token) {
  return http.post('/sys/sso/getToken?token=' + token, null, {
    needToken: false,
    showLoading: false
  })
}

/**
 * 短信验证码登录
 */
export function loginBySmsCode (params) {
  return http.post('/sys/sso/phoneLogin', {
    phone: params.phoneNumber,
    code: params.smsCode
  }, {
    needToken: false
  })
}

/**
 * 切换登录用户所在单位
 */
export function changeLoginUnit (unitUserId) {
  return http.get(`/sys/sso/changeUnit/${unitUserId}`)
}

/**
 * 切换代理
 */
export function changeLoginAgent (params) {
  return http.post('/sys/sso/agentLogin', {
    principalBy: params.principalBy,
    agentBy: params.agentBy
  }, {
    needToken: false
  })
}

/**
 * 获取登录验证码
 * @returns
 */
export function getCaptchaImage (responseType) {
  return http.get('/captcha/captchaImage', {
    needToken: false,
    responseType: responseType || 'arraybuffer'
  })
}

/**
 * 获取短信验证码
 * @param phoneNumber 手机号
 */
export function getSmsCode (phoneNumber, validateCode, params, baseURL) {
  params = params || {}
  params = Object.assign(params, {
    phone: phoneNumber,
    validateCode
  })
  return http.post('/sys/sso/sendPhoneCode', params, {
    needToken: false,
    headers: {
      validateCode
    }
  })
}

/**
* 获取用户菜单
*
*/
/**
 * 获取用户菜单权限
 * @returns
 */
export function getUserMenu () {
  const clientType = store.getAppConfig('_clientType'),
    currentAppList = UrlUtil.getURLParam('currentAppList')
  return http.get(`/sys/sso/userMenuByCache?client=${client[clientType]}&currentAppList=${currentAppList || ''}`, {
    showLoading: false
  })
}

/**
 * 获取用户全部菜单权限
 */
export function getAllUserMenu (params = {}) {
  return http.get(`/sys/sso/userMenuByCache?category=&currentAppList=&useType=${params.useType}`)
}

/**
 * 检查token
 * @returns
 */
export function isValidToken () {
  return http.get(`/sys/sso/tokenvalid`)
}